<html>
<head><meta charset="utf-8"><title>How can I look up traits in scope across crates? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20can.20I.20look.20up.20traits.20in.20scope.20across.20crates.3F.html">How can I look up traits in scope across crates?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="205433230"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20can%20I%20look%20up%20traits%20in%20scope%20across%20crates%3F/near/205433230" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20can.20I.20look.20up.20traits.20in.20scope.20across.20crates.3F.html#205433230">(Jul 30 2020 at 01:37)</a>:</h4>
<p>From <a href="#narrow/stream/131828-t-compiler/topic/New.20query.20for.20traits.20that.20a.20type.20implements/near/205432986">https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/New.20query.20for.20traits.20that.20a.20type.20implements/near/205432986</a>:</p>
<p>I have code that looks like</p>
<div class="codehilite"><pre><span></span><code><span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">module</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tcx</span><span class="p">.</span><span class="n">hir</span><span class="p">().</span><span class="n">as_local_hir_id</span><span class="p">(</span><span class="n">module</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">traits_in_scope</span>: <span class="nc">FxHashSet</span><span class="o">&lt;</span><span class="n">_</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">match</span><span class="w"> </span><span class="n">tcx</span><span class="p">.</span><span class="n">in_scope_traits</span><span class="p">(</span><span class="n">module</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nb">Some</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">x</span><span class="p">.</span><span class="n">iter</span><span class="p">().</span><span class="n">map</span><span class="p">(</span><span class="o">|</span><span class="n">candidate</span><span class="o">|</span><span class="w"> </span><span class="n">candidate</span><span class="p">.</span><span class="n">def_id</span><span class="p">).</span><span class="n">collect</span><span class="p">(),</span><span class="w"></span>
<span class="w">        </span><span class="nb">None</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">debug</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;in_scope_traits returned None&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">            </span><span class="k">return</span><span class="w"> </span><span class="o">&amp;</span><span class="p">[];</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>
</code></pre></div>


<p>It turns out the <code>None</code> case is being hit an awful lot and it's breaking my other changes. Does anyone know <em>why</em> in_scope_traits would return None?</p>



<a name="205433304"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20can%20I%20look%20up%20traits%20in%20scope%20across%20crates%3F/near/205433304" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20can.20I.20look.20up.20traits.20in.20scope.20across.20crates.3F.html#205433304">(Jul 30 2020 at 01:38)</a>:</h4>
<p>for context, this is being called on</p>
<div class="codehilite"><pre><span></span><code><span class="cp">#![deny(intra_doc_link_resolution_failure)]</span><span class="w"></span>

<span class="sd">/// [`std::collections::BTreeMap::into_iter`]</span>
<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
</code></pre></div>


<p>where the <code>module</code> is the module of <code>foo</code>. So it's not as if the module is unknown.</p>



<a name="205745848"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20can%20I%20look%20up%20traits%20in%20scope%20across%20crates%3F/near/205745848" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20can.20I.20look.20up.20traits.20in.20scope.20across.20crates.3F.html#205745848">(Aug 02 2020 at 21:59)</a>:</h4>
<p>I think this is related to <code>BTreeMap</code> being in a separate crate</p>



<a name="205745892"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20can%20I%20look%20up%20traits%20in%20scope%20across%20crates%3F/near/205745892" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20can.20I.20look.20up.20traits.20in.20scope.20across.20crates.3F.html#205745892">(Aug 02 2020 at 22:00)</a>:</h4>
<p>is there a way to get traits in scope <em>across crates</em>? <code>resolve_ast_path</code> already does this so the data is there</p>



<a name="205745900"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20can%20I%20look%20up%20traits%20in%20scope%20across%20crates%3F/near/205745900" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20can.20I.20look.20up.20traits.20in.20scope.20across.20crates.3F.html#205745900">(Aug 02 2020 at 22:00)</a>:</h4>
<p>the reason I don't want to use resolve_ast_path is because it's super inefficient to first look at all traits and only then filter by whether they're in scope, just resolving the name is expensive</p>



<a name="205745982"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20can%20I%20look%20up%20traits%20in%20scope%20across%20crates%3F/near/205745982" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20can.20I.20look.20up.20traits.20in.20scope.20across.20crates.3F.html#205745982">(Aug 02 2020 at 22:03)</a>:</h4>
<p>looks like the map is only populated for some expressions</p>



<a name="205746031"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20can%20I%20look%20up%20traits%20in%20scope%20across%20crates%3F/near/205746031" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20can.20I.20look.20up.20traits.20in.20scope.20across.20crates.3F.html#205746031">(Aug 02 2020 at 22:05)</a>:</h4>
<p>ah, no, it's being rewritten to contain HirOwners as keys</p>



<a name="205746033"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20can%20I%20look%20up%20traits%20in%20scope%20across%20crates%3F/near/205746033" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20can.20I.20look.20up.20traits.20in.20scope.20across.20crates.3F.html#205746033">(Aug 02 2020 at 22:05)</a>:</h4>
<p>ooh is that an in-progress PR? have a link?</p>



<a name="205746034"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20can%20I%20look%20up%20traits%20in%20scope%20across%20crates%3F/near/205746034" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20can.20I.20look.20up.20traits.20in.20scope.20across.20crates.3F.html#205746034">(Aug 02 2020 at 22:05)</a>:</h4>
<p>try passing <code>foo</code>s ID instead of its module</p>



<a name="205746073"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20can%20I%20look%20up%20traits%20in%20scope%20across%20crates%3F/near/205746073" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20can.20I.20look.20up.20traits.20in.20scope.20across.20crates.3F.html#205746073">(Aug 02 2020 at 22:06)</a>:</h4>
<p>no, I mean the code rewrites the map</p>



<a name="205746076"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20can%20I%20look%20up%20traits%20in%20scope%20across%20crates%3F/near/205746076" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20can.20I.20look.20up.20traits.20in.20scope.20across.20crates.3F.html#205746076">(Aug 02 2020 at 22:06)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/blob/19cefa68640843956eedd86227ddc1d35dbc6754/src/librustc_middle/ty/context.rs#L1119-L1123">https://github.com/rust-lang/rust/blob/19cefa68640843956eedd86227ddc1d35dbc6754/src/librustc_middle/ty/context.rs#L1119-L1123</a></p>



<a name="205746193"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20can%20I%20look%20up%20traits%20in%20scope%20across%20crates%3F/near/205746193" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20can.20I.20look.20up.20traits.20in.20scope.20across.20crates.3F.html#205746193">(Aug 02 2020 at 22:10)</a>:</h4>
<p>getting <code>foo</code>s id is going to take me a while</p>



<a name="205746195"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20can%20I%20look%20up%20traits%20in%20scope%20across%20crates%3F/near/205746195" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20can.20I.20look.20up.20traits.20in.20scope.20across.20crates.3F.html#205746195">(Aug 02 2020 at 22:10)</a>:</h4>
<p>rustdoc keeps throwing away info <span aria-label="angry" class="emoji emoji-1f620" role="img" title="angry">:angry:</span></p>



<a name="205746249"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20can%20I%20look%20up%20traits%20in%20scope%20across%20crates%3F/near/205746249" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20can.20I.20look.20up.20traits.20in.20scope.20across.20crates.3F.html#205746249">(Aug 02 2020 at 22:12)</a>:</h4>
<p>this will still only work for local items in any case :/</p>



<a name="205746255"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20can%20I%20look%20up%20traits%20in%20scope%20across%20crates%3F/near/205746255" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20can.20I.20look.20up.20traits.20in.20scope.20across.20crates.3F.html#205746255">(Aug 02 2020 at 22:13)</a>:</h4>
<p>because it takes a HirId</p>



<a name="205746515"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20can%20I%20look%20up%20traits%20in%20scope%20across%20crates%3F/near/205746515" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20can.20I.20look.20up.20traits.20in.20scope.20across.20crates.3F.html#205746515">(Aug 02 2020 at 22:21)</a>:</h4>
<p>more context in case it helps: <a href="https://github.com/rust-lang/rust/pull/74489#discussion_r464125208">https://github.com/rust-lang/rust/pull/74489#discussion_r464125208</a></p>



<a name="205975973"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20can%20I%20look%20up%20traits%20in%20scope%20across%20crates%3F/near/205975973" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20can.20I.20look.20up.20traits.20in.20scope.20across.20crates.3F.html#205975973">(Aug 05 2020 at 00:10)</a>:</h4>
<p>ok it seems that <code>in_scope_traits()</code> as written won't work, but is there an intrinsic reason it only works for the current crate? Could I modify it to store <code>DefId</code>s instead?</p>



<a name="205975991"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20can%20I%20look%20up%20traits%20in%20scope%20across%20crates%3F/near/205975991" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20can.20I.20look.20up.20traits.20in.20scope.20across.20crates.3F.html#205975991">(Aug 05 2020 at 00:10)</a>:</h4>
<p>and somewhat related - is there a way to tell if a given DefId is in scope for any given module?</p>



<a name="205976018"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20can%20I%20look%20up%20traits%20in%20scope%20across%20crates%3F/near/205976018" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20can.20I.20look.20up.20traits.20in.20scope.20across.20crates.3F.html#205976018">(Aug 05 2020 at 00:11)</a>:</h4>
<p>I found <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_resolve/struct.Resolver.html#method.resolve_ident_in_module">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_resolve/struct.Resolver.html#method.resolve_ident_in_module</a> but it doesn't seem to work cross-crate</p>



<a name="205976699"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20can%20I%20look%20up%20traits%20in%20scope%20across%20crates%3F/near/205976699" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20can.20I.20look.20up.20traits.20in.20scope.20across.20crates.3F.html#205976699">(Aug 05 2020 at 00:22)</a>:</h4>
<p>found it! <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/enum.Visibility.html#method.is_accessible_from">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/enum.Visibility.html#method.is_accessible_from</a></p>



<a name="205976821"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20can%20I%20look%20up%20traits%20in%20scope%20across%20crates%3F/near/205976821" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20can.20I.20look.20up.20traits.20in.20scope.20across.20crates.3F.html#205976821">(Aug 05 2020 at 00:24)</a>:</h4>
<p>nope, that just tells me if it <em>could be</em> in scope :(</p>



<a name="206210159"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20can%20I%20look%20up%20traits%20in%20scope%20across%20crates%3F/near/206210159" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20can.20I.20look.20up.20traits.20in.20scope.20across.20crates.3F.html#206210159">(Aug 06 2020 at 23:37)</a>:</h4>
<p>I'm still stuck on this :/ <code>rustc_resolve</code> works cross-crate but it can only look things up by name, not by DefId, and the trait could have been renamed</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>